package com.lqg.bookLibrary.adminController;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lqg.bookLibrary.common.core.domain.JsonResult;
import com.lqg.bookLibrary.pojo.Books;
import com.lqg.bookLibrary.pojo.Menu;
import com.lqg.bookLibrary.pojo.Users;
import com.lqg.bookLibrary.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/manage/menu")
@PreAuthorize("hasAnyRole('a')")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @GetMapping("/side")
    public JsonResult read() {
        List<Menu> list = menuService.list();
        Map<Integer, JSONObject> map = new HashMap<>();
        for (Menu menu : list) {
            if (menu.getFatherId() == null) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", menu.getMenuId());
                jsonObject.put("index", menu.getMenuId());
                jsonObject.put("name", menu.getMenuName());
                jsonObject.put("path", menu.getPath());
                map.put(menu.getMenuId(), jsonObject);
            } else {
                JSONObject jsonObject = map.get(menu.getFatherId());
                JSONObject child = new JSONObject();
                child.put("id", menu.getFatherId()+"."+menu.getMenuId());
                child.put("index", menu.getFatherId()+"-"+menu.getMenuId());
                child.put("name", menu.getMenuName());
                child.put("path", menu.getPath());
                child.put("query", menu.getQuery());
                if (!jsonObject.containsKey("children")) {
                    List<JSONObject> list1 = new ArrayList<>();
                    list1.add(child);
                    jsonObject.put("children", list1);
                }else {
                    List<JSONObject> children = (List)jsonObject.get("children");
                    children.add(child);
                }
            }
        }

        //List<JSONObject> result = new ArrayList<>();

        return JsonResult.success(map.values());
    }

    @GetMapping("/read/{pageNum}")
    public JsonResult read(@PathVariable int pageNum) {
        long count = menuService.count();
        int maxCun = 20;
        int chu = (pageNum-1)*maxCun;
        List<Menu> list = menuService.lambdaQuery()
                .last("limit " + chu + "," + maxCun)
                .list();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", list);
        jsonObject.put("pages", (int)(count/maxCun)+1);
        jsonObject.put("nowPage", pageNum);

        return JsonResult.success(jsonObject);
    }


    @PostMapping("/update")
    public JsonResult update(@RequestBody String post){
        Menu menu = JSON.parseObject(post, Menu.class);
        menuService.updateById(menu);
        Menu one1 = menuService.lambdaQuery().eq(Menu::getMenuId, menu.getMenuId()).one();
        return JsonResult.success(one1);
    }


    @GetMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id){
        boolean b = menuService.removeById(id);
        if (b) {
            return JsonResult.success();
        }else {
            return JsonResult.fail("删除失败");
        }
    }


    @PostMapping("/insert")
    public JsonResult insert(@RequestBody String post){
        Menu menu = JSON.parseObject(post, Menu.class);
        menuService.save(menu);
        Menu one = menuService.lambdaQuery()
                .eq(Menu::getMenuName, menu.getMenuName())
                .one();
        return JsonResult.success(one);
    }

    @PostMapping("/search")
    public JsonResult search(@RequestBody String post){
        JSONObject jsonObject = JSONObject.parseObject(post);
        String condition = jsonObject.get("condition").toString();
        String content = jsonObject.get("content").toString();
        //System.out.printf(condition + "     " + content);

        condition = StrUtil.toUnderlineCase(condition);

        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        wrapper.eq(condition, content);
        List<Menu> list = menuService.list(wrapper);

        JSONObject result = new JSONObject();
        result.put("data", list);
        result.put("pages", 1);
        result.put("nowPage", 1);

        return JsonResult.success(result);
    }

}
